package com.bitters.core.exception;

import com.bitters.enums.ErrorCodeAndMsg;
import com.bitters.message.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author thomashe
 * @date 2023/10/20/ 16:16
 * @description 统一拦截异常
 */
//@RestControllerAdvice
public class ExceptionHandler {
    public static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    @org.springframework.web.bind.annotation.ExceptionHandler(BizRuntimeException.class)
    @ResponseBody
    public Response handleStudentException(HttpServletRequest request, BizRuntimeException ex) {
        Response response;
        logger.error("BizRuntimeException code:{},msg:{}", ex.getResponse().getCode(), ex.getResponse().getMsg());
        response = new Response(ex.getResponse().getCode(), ex.getResponse().getMsg());
        return response;
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public Response handleException(HttpServletRequest request, Exception ex) {
        Response response;
        logger.error("exception error:{}", ex);
        response = new Response(ErrorCodeAndMsg.Network_error.getCode(), ErrorCodeAndMsg.Network_error.getMsg());
        return response;

    }
}
